package userInterface;
import javax.swing.*;

import assembuler.Assembuler;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.io.*;
public class MainToolBar extends JPanel implements ActionListener {
    static final private String ACTION_NEW = "new";
    static final private String ACTION_OPEN = "open";
    static final private String ACTION_EXECUTE = "execute";
    static final private String ACTION_EXPORT = "export";
    static final private String ACTION_QUIT = "quit";
    public MainToolBar() {
        super(new BorderLayout());

        JToolBar toolBar=new JToolBar("MainToolBar");
        addButtons(toolBar);
        toolBar.setFloatable(false);
        
        setPreferredSize(new Dimension(400,70));
        add(toolBar,BorderLayout.PAGE_START);
    }

    protected void addButtons(JToolBar toolBar) {
        JButton button=null;
        button = makeNavigationButton("images/new.gif",ACTION_NEW,"New a file","New");
        toolBar.add(button);
        button = makeNavigationButton("images/execute.gif",ACTION_EXECUTE,"Execute the Logo Script","Execute");
        toolBar.add(button);
        button = makeNavigationButton("images/open.gif",ACTION_OPEN,"Open the LOG file","Open");
        toolBar.add(button);
        button = makeNavigationButton("images/export.gif",ACTION_EXPORT,"Export the Panel","Export");
        toolBar.add(button);
        button = makeNavigationButton("images/quit.gif",ACTION_QUIT,"Quit","Quit");
        toolBar.add(button);
    }
    protected JButton makeNavigationButton(String imgLocation,
            String actionCommand,
            String toolTipText,
            String altText) {
        //Look for the image.

        //Create and initialize the button.
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(this);

        button.setMaximumSize(new Dimension(60,60));
        URL tmp=MainToolBar.class.getResource(imgLocation);
        if (tmp != null)
        	button.setIcon(new ImageIcon(tmp));
        else
        	System.err.println(imgLocation+"NOT FOUND!");

        return button;
    }
    
    
    
    
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals(ACTION_NEW)) {
        	EnvironmentVariables.editorList.add(new LogoScriptEditorFrame(String.format("LogoScript %d", EnvironmentVariables.editorList.size()+1),null));
        }
        if (cmd.equals(ACTION_OPEN)) {
        	File filePath;
        	JFileChooser openFileChooser=new JFileChooser(".");
        	openFileChooser.setAcceptAllFileFilterUsed(false);
        	openFileChooser.addChoosableFileFilter(new ImageFileFilter("logo"));
        	int result=openFileChooser.showOpenDialog(EnvironmentVariables.mainFrame);
        	if (result==JFileChooser.APPROVE_OPTION) {
        		filePath=openFileChooser.getSelectedFile();
        		EnvironmentVariables.editorList.add(new LogoScriptEditorFrame(filePath.getName(),filePath));
        	}
        }
        if (cmd.equals(ACTION_QUIT))
        	System.exit(0);
        if (cmd.equals(ACTION_EXPORT)) {
        	JFileChooser saveFileChooser=new JFileChooser(".");
        	saveFileChooser.setAcceptAllFileFilterUsed(false);
        	saveFileChooser.addChoosableFileFilter(new ImageFileFilter("bmp"));
        	int result=saveFileChooser.showSaveDialog(EnvironmentVariables.mainFrame);
        	if (result==JFileChooser.APPROVE_OPTION) {
        		File imageOutputFile=saveFileChooser.getSelectedFile();
        		try {
        			if (!imageOutputFile.exists())
        				imageOutputFile.createNewFile();
        			CanvasExporter.exportImage(imageOutputFile, EnvironmentVariables.mainPanel.canvasImage,"bmp");
        		} catch (IOException e1) {
        			e1.printStackTrace();
        		}
        		System.out.format("export image to %s",imageOutputFile.getAbsolutePath());
        	}
        }
        if (cmd.equals(ACTION_EXECUTE)) {
        	JFileChooser executeFileChooser=new JFileChooser(".");
        	executeFileChooser.setAcceptAllFileFilterUsed(false);
        	executeFileChooser.addChoosableFileFilter(new ImageFileFilter("logo"));
        	int result=executeFileChooser.showOpenDialog(EnvironmentVariables.mainFrame);
        	if (result==JFileChooser.APPROVE_OPTION) {
        		String executeFilePath=executeFileChooser.getSelectedFile().getAbsolutePath();
        		Assembuler.executeCodeFromFile(executeFilePath);
        	}
        }
    }


}
